package com.superbytecode.stencil.enums;

import com.superbytecode.stencil.utils.StringUtil;
import lombok.Getter;

import java.util.stream.Stream;

/**
 * All rights Reserved, Designed By www.super-master.com
 *
 * @ProjectName: code-stencil
 * @Package: com.superbytecode.stencil.definitions
 * @ClassName: DatabaseTypeEnum
 * @Description: [数据库类型]
 * <p>  数据库种类+STL唯一确定一个枚举值 </p>
 * @Author: [Lance Ting]
 * @Date: 2024/10/11 14:13
 * @Version: V1.0
 * @Copyright: 2024 www.super-master.cn Inc. All rights reserved.
 * TODO: 注意,本文件Lance Ting所作,如果转载或使用请标明具体出处!
 **/
@Getter
public enum DatabaseTypeEnum {
    /**
     * mysql:
     * <p>version: 5.7 | 8.0 </p>
     */
    MYSQL5("mysql", "5.7", "", //
            ""),//
    MYSQL8("mysql", "8.0", "com.mysql.cj.jdbc.Driver", //
            "jdbc:mysql://{db_host}:{db_port}/{db_name}?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false&allowPublicKeyRetrieval=true"),//
    /**
     * oracle:
     * <p>version: 9i | 10g | 11g | 12c </p>
     */
    ORACLE11("oracle", "11g", "oracle.jdbc.OracleDriver", //
            "jdbc:oracle:thin:@{db_host}:{db_port}/{db_name}"),//

    /**
     * postgresql:
     * <p>version: 9.6 | 12.0 | 13.0 </p>
     */
    POSTGRESQL12("postgresql", "12.0", "org.postgresql.Driver", //
            "jdbc:postgresql://{db_host}:{db_port}/{db_name}?currentSchema=schema"),//
    /**
     * sqlserver:
     * <p>version: 2008 R2 | 2012 | 2014 </p>
     */
    SQLSERVER2012("sqlserver", "2012", "com.microsoft.sqlserver.jdbc.SQLServerDriver", //
            "jdbc:sqlserver://{db_host}:{db_port};databaseName={db_name}"),//
    ;

    // -----------------------------------------------------------------------------------------------------------------
    public static DatabaseTypeEnum of(String code, String stl) {
        return Stream.of(values())
                // 过滤编码和版本号
                .filter(c -> StringUtil.equalsIgnoreCase(c.getCategoryCode(), c.getCategoryLTS(), code, stl))
                // 如果匹配到返回，否则不支持数据库类型和STL版本
                .findAny() //
                .orElseThrow(() -> new IllegalArgumentException(String.format("Not support database type: %s_%s", code, stl)));
    }
    // -----------------------------------------------------------------------------------------------------------------
    /**
     * 数据库类型编码
     * <p>值：数据类型-大版本号</p>
     */
    final private String categoryCode;
    /**
     * 数据库长期稳定大版本
     */
    final private String categoryLTS;
    /**
     * 数据库驱动名称
     */
    final private String driverClass;

    /**
     * jdbcUrl格式：{db_host}:{db_port}/{db_name}
     * <p>
     * oracle格式: oracle: jdbc:oracle:thin:@{db_host}:{db_port}:{db_name}
     * </p>
     */
    final private String urlPattern;

    DatabaseTypeEnum(String categoryCode, String categoryLTS, String driverClass, String urlPattern) {
        this.categoryCode = categoryCode;
        this.categoryLTS = categoryLTS;
        this.driverClass = driverClass;
        this.urlPattern = urlPattern;
    }
}